var initContext = function(context) {
    var testingZone = document.querySelector('#testing-zone');
    testingZone.innerHTML = document.querySelector(context).innerHTML;
};

var fireEvent = function(element,event) {
    if (document.createEvent) {
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    } else { // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt);
    }
};


module('Under the hood');

test("sha1 generation", function() {
    // Given
    var sha1 = new UniquePasswordBuilder.Sha1();

    // When
    var generatedHash = sha1.sha1Hash("unique password builder");

    // Then
    ok( generatedHash === "483fa7ed180439e0ddf72712b18ae84c821cd193", "sha1 generation problem" );
});

test("makeHashHumanReadable", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'notrelevant.com'});

    // When
    var generatedPassword = upb.makeHashHumanReadable("483fa7ed180439e0ddf72712b18ae84c821cd193");

    // Then
    ok( generatedPassword === "qpxDeR2w1B", "makeHashHumanReadble transformation problem" );
});

test("generateUniquePassword for https://github.com with 'default-password'", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'github.com'});

    // When
    var generatedPassword = upb.generateUniquePassword('default-password');

    // Then
    ok( generatedPassword === "4F9DaeVJQ0", "generateUniquePassword problem" );
});

test("generateUniquePassword for http://github.com with 'default-password'", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'http:',host:'github.com'});

    // When
    var generatedPassword = upb.generateUniquePassword('default-password');

    // Then
    ok( generatedPassword === "0tZlCG.NjB", "generateUniquePassword problem" );
});

test("generateUniquePassword for https://www.google.com with 'default-password'", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'www.google.com'});

    // When
    var generatedPassword = upb.generateUniquePassword('default-password');

    // Then
    ok( generatedPassword === "J;.yszKRCY", "generateUniquePassword problem" );
});

test("generateUniquePassword for https://www.google.com with 'default-password2'", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'www.google.com'});

    // When
    var generatedPassword = upb.generateUniquePassword('default-password2');

    // Then
    ok( generatedPassword === "$.hmsiAt24", "generateUniquePassword problem" );
});

test("generateUniquePassword for https://www.google.com with 'default-password' with 2 rounds", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'www.google.com'}, 2);

    // When
    var generatedPassword = upb.generateUniquePassword('default-password');

    // Then
    ok( generatedPassword === "BZp38u:4xz", "generateUniquePassword problem" );
});

test("generateUniquePassword for https://www.google.com with 'default-password' with 10 rounds", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'www.google.com'}, 10);

    // When
    var generatedPassword = upb.generateUniquePassword('default-password');

    // Then
    ok( generatedPassword === ":GEiwSxLXa", "generateUniquePassword problem" );
});

module('Should add link action');

test("login normal case", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'domain.com'});
    initContext('#case-login');

    // When
    upb.insertGenerateActions();

    // Then
    ok( document.querySelectorAll('#testing-zone input[type=password] + a.uniquePasswordBuilder').length == 1, "insertGenerateActions() didn’t add a link !" );
});

test("login case with invisible fields", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'domain.com'});
    initContext('#case-login-with-invisible-fields');

    // When
    upb.insertGenerateActions();

    // Then
    ok( document.querySelectorAll('#testing-zone input[type=password] + a.uniquePasswordBuilder').length == 7, "insertGenerateActions() didn’t add links !" );
});


test("password creation case with 3 fields : old, new and confirmation", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'domain.com'});
    initContext('#case-creation-3-fields');

    // When
    upb.insertGenerateActions();

    // Then
    ok( document.querySelectorAll('#testing-zone input[type=password] + a.uniquePasswordBuilder').length == 3, "insertGenerateActions() didn’t add a link !" );
});


module('Clicking on link should generate unique password and fill it with the current password');

test("login normal case", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'domain.com'});
    initContext('#case-login');

    // When
    upb.insertGenerateActions();
    var field = document.querySelector('#testing-zone input[type=password]');
    field.value = "default-password";
    var link = document.querySelector('#testing-zone input[type=password] + a.uniquePasswordBuilder');
    fireEvent(link, 'click');

    // Then
    ok( field.value === "BAIXav;-Ta", "Clicking on the link didn’t do anything");
});

test("password creation case with 3 fields : old, new and confirmation", function() {
    // Given
    var upb = new UniquePasswordBuilder.UniquePasswordBuilder({protocol:'https:',host:'domain.com'});
    initContext('#case-creation-3-fields');

    // When
    upb.insertGenerateActions();
    var fields = document.querySelectorAll('#testing-zone input[type=password]');
    fields[0].value = "old-password";
    fields[1].value = "default-password";
    fields[2].value = "default-password";
    var links = document.querySelectorAll('#testing-zone input[type=password] + a.uniquePasswordBuilder');
    fireEvent(links[0], 'click');
    fireEvent(links[1], 'click');
    fireEvent(links[2], 'click');

    // Then
    ok( fields[0].value === "WdFRQ9z8EV", "Clicking on the link didn’t do anything");
    ok( fields[1].value === "BAIXav;-Ta", "Clicking on the link didn’t do anything");
    ok( fields[2].value === "BAIXav;-Ta", "Clicking on the link didn’t do anything");
});

